require("dplyr")

## Set working directory
## to Dataverse folder

survey <- read.table("Harris_Data/Harris 1999 Business Week National Issues Survey, study no. 11581/harris_s11581_spss.tab", header = TRUE)


# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- "11581"

# study year (year)
survey$year <- 1999

# geographic data (urban)
table(survey$SOP)
survey$urban <- dplyr::recode(survey$SOP,
                       `1` = "Urban",
                       `2` = "Suburban",
                       `3` = "Rural")
table(survey$urban)

# geographic data (region)
table(survey$REGION)
survey$region <- dplyr::recode(survey$REGION,
                        `1` = "East",
                        `2` = "East",
                        `3` = "South",
                        `4` = "South",
                        `5` = "Midwest",
                        `6` = "Midwest",
                        `7` = "West",
                        `8` = "West")
table(survey$region)

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$Q406A2)
## interestingly doesn't match up with codebook 
## but this makes the most sense

survey$inequality <- dplyr::recode(as.character(survey$Q406A2),
                            `1` = "Feel",
                            `0` = "Don't Feel",
                            `-8` = "Not Sure",
                            `-9` = "Refused")
#survey$inequality[survey$inequality == "Refused" & !is.na(survey$inequality)] <- NA

table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- NA
survey$union.other <- NA

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- NA

# occ self
survey$occupation.self <- NA

# household size (hhsize)
## this one only includes adults 
table(survey$Q2005)
survey$hhsize <- as.character(survey$Q2005)
survey$hhsize[survey$hhsize < 0] <- NA
table(survey$hhsize)

# education (educ)
table(survey$Q2020)
survey$educ <- dplyr::recode(survey$Q2020,
                      `1` = "Less than high school",
                      `2` = "Completed some high school",
                      `3` = "High school graduate",
                      `4` = "Some college",
                      `5` = "College graduate",
                      `6` = "Some graduate school",
                      `7` = "Completed post graduate",
                      `-8` = "Don't know",
                      `-9` = "Refused")
table(survey$educ)

## stopped here 
# household income (income)
table(survey$Q2050)
table(survey$Q2052) ## less than 50000
table(survey$Q2053) ## greater than 50000

table(survey$Q2052[survey$Q2050 == 2])
table(survey$Q2050[survey$Q2052 == -99.99]) ## so this value on table for less than 50000 includes the don't know, refuse
## from the survey flow question 

table(survey$Q2053[survey$Q2050 == 1])
table(survey$Q2050[survey$Q2053 == -99.99]) ## again this value includes don't know, refuse for original question

## create special variable to indicate "don't know"/"refused" on initial question
## this is to separate out these values from the values that say they answered the other question
survey$Q2052[survey$Q2050 < 0] <- -100
survey$Q2053[survey$Q2050 < 0] <- -100

## take the income data only for those making less than $50k
survey$income <- dplyr::recode(survey$Q2052,
                        `1` = "$0 to $14,999",
                        `2` = "$15,000 to $24,999",
                        `3` = "$25,000 to $34,999",
                        `4` = "$35,000 to $49,999",
                        `-8` = "Not sure",
                        `-9` = "Refused",
                        `-99.99` = "Over $50,000",
                        `-100` = "Disregard") # disregarding DK/refused
table(survey$income)

## combine with income data for those making more than $50k
## do this by only using the points where <$50k is NOT available
survey$income[survey$income == "Over $50,000"] <- survey$Q2053[survey$Q2052 == -99.99]
table(survey$income)

## recode >$50k data
survey$income <- dplyr::recode(survey$income,
                        `1` = "$50,000 to $74,999",
                        `2` = "$75,000 to $99,999",
                        `3` = "$100,000 to $124,999",
                        `4` = "$125,000 to $149,999",
                        `5` = "$150,000 to $199,999",
                        `6` = "$200,000 to $249,999",
                        `7` = "$250,000 or more",
                        `-8` = "Not sure",
                        `-9` = "Refused")
table(survey$income)

## get rid of initial data that was supposed to be disregarded
survey$income[survey$income == "Disregard"] <- NA
table(survey$income)

# age
table(survey$Q2010)
survey$age <- as.character(survey$Q2010)
survey$age[survey$age < 0] <- NA
table(survey$age)

# race
table(survey$Q2070)
survey$race1 <- dplyr::recode(survey$Q2070,
                      `1` = "White",
                      `2` = "Black",
                      `3` = "African-American",
                      `4` = "Asian or Pacific Islander",
                      `5` = "American Indian or Alaskan native",
                      `6` = "Mixed race",
                      `7` = "Some other race",
                      `-8` = "Not sure",
                      `-9` = "Refused")
survey$race2 <- dplyr::recode(survey$Q2060,
                              `-9` = "Decline/not sure",
                              `-8` = "Decline/not sure",
                              `1` = "Yes, hispanic",
                              `0` = "No, not hispanic")
table(survey$race1)
table(survey$race2)
survey$race <- ifelse(survey$race1 == "Refused" |
                        survey$race1 == "Not sure", 
                      "Decline/not sure", ifelse(survey$race1 == "White",
                                                 ifelse(survey$race2 == "Decline/not sure",
                                                        "Decline/not sure",
                                                        ifelse(survey$race2 == "No, not hispanic",
                                                               "Non-Hispanic White",
                                                               "Hispanic White")), "Non-white"))
table(survey$race)
table(survey$race[survey$race1 == "White"])

# politics (party)
table(survey$Q2043)
survey$party <- dplyr::recode(survey$Q2043,
                       `1` = "Republican",
                       `2` = "Democrat",
                       `3` = "Independent",
                       `4` = "Other",
                       `-8` = "Not sure",
                       `-9` = "Refused")
table(survey$party)

# politics (ideology)
table(survey$Q2045)
survey$ideology <- dplyr::recode(survey$Q2045,
                          `1` = "Conservative",
                          `2` = "Moderate",
                          `3` = "Liberal",
                          `-8` = "Not sure",
                          `-9` = "Refused")
table(survey$ideology)

# gender
table(survey$Q2110)
survey$gender <- dplyr::recode(survey$Q2110,
                        `1` = "Male",
                        `2` = "Female")
table(survey$gender)

# religion
survey$religion <- NA

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
table(survey$Q406A1)

survey$dontcare <- dplyr::recode(survey$Q406A1,
                                 `1` = "Feel",
                                 `2` = "Don't Feel",
                                 `3` = "Not Sure",
                                 `4` = "Refused")
survey$dontcount <- dplyr::recode(survey$Q406A3,
                                  `1` = "Feel",
                                  `0` = "Don't Feel",
                                  `-8` = "Not Sure",
                                  `-9` = "Refused")
survey$leftout <- dplyr::recode(survey$Q406A4,
                                `1` = "Feel",
                                `0` = "Don't Feel",
                                `-8` = "Not Sure",
                                `-9` = "Refused")

## question place
survey$question_place <- "before party"

# subset
survey_11581 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                          "inequality", "inequality.variable", "union.self", "union.other",
                          "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                          "age", "race", "party", "ideology", "gender", "religion",
                          "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                          "question_place")]


# save file
#saveRDS(survey_11581, file = "Harris_Data/survey_11581.rds")

